package com.jzg.jcptadmin.recevier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.jzg.jcptadmin.Utils.MyToast;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.data.vo.PushRcevierData;
import com.jzg.jcptadmin.data.vo.User;
import com.jzg.jcptadmin.ui.HomeActivity;
import com.jzg.jcptadmin.ui.KuaiSuOnLineActivity;
import com.jzg.jcptadmin.ui.OfflineDetectionActivity;
import com.orhanobut.logger.Logger;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class PushRecevier extends BroadcastReceiver {
    private static final String TAG = "JPush";
    private PushRcevierData pushRcevierData;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            //点击了通知栏
            try {
                //0 待检测(待评估\线上转线下\线下转到线上) 快专，1 已确认(已评估\驳回) 快专，2已检测，3超时，4异常(退回)，5已审核，6已签收
                Gson gson = new Gson();
                String string = bundle.getString(JPushInterface.EXTRA_EXTRA);
                PushRcevierData pushRcevierData = gson.fromJson(string, PushRcevierData.class);
                Logger.e(pushRcevierData.toString());
                Intent i;
                if (pushRcevierData != null && pushRcevierData.getTaskType() != null) {
                    AppContext appContext = (AppContext) context.getApplicationContext();
                    User user = appContext.getUser();
                    if (user == null) {
                        MyToast.showShort( "请先登录");
                        return;
                    }
                    if (pushRcevierData.getTaskType().equals("1")) {  //1快速线上列表2专业线下列表3被驳回列表
                        if (user.getIsDL().equals("0")) {
                            i = new Intent(context, KuaiSuOnLineActivity.class);
                        } else {
                            return;
                        }

                    } else if (pushRcevierData.getTaskType().equals("2")) {
                        if (user.getIsDL().equals("0")) {
                            i = new Intent(context, OfflineDetectionActivity.class);
                        } else {
                            return;
                        }
                    } else {
//                        if (user.getIsDL().equals("1")) {
//                            i = new Intent(context, YiXinActivity.class);
//                            i.putExtra("title", "被驳回");
//                        } else {
//                            return;
//                        }
                        i = new Intent(context, HomeActivity.class);

                    }
                } else {
                    i = new Intent(context, HomeActivity.class);
                }
                //打开自定义的Activity
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            } catch (Exception e) {
                e.printStackTrace();
                Intent intent2 = new Intent(context, HomeActivity.class);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(intent2);
            }
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }


}
